package com.test.controller.exception;

import com.test.entity.RestBean;
import jakarta.validation.ValidationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class Exception {

    @ExceptionHandler(ValidationException.class)
    public RestBean<String> handleValidationException(ValidationException e) {
        log.warn("Resolve [{}:{}]", e.getClass().getName(), e.getMessage());
        return RestBean.failure(HttpStatus.BAD_REQUEST.value(), "请求参数有误");
    }

    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public RestBean<String> handleMissingParameterException(MissingServletRequestParameterException e) {
        log.warn("Resolve [{}:{}]", e.getClass().getName(), e.getMessage());
        return RestBean.failure(HttpStatus.BAD_REQUEST.value(), "缺少必需的请求参数: " + e.getParameterName());
    }
}
